package cn.ffcs.uec.outStaff.component;

import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.util.media.Media;
import org.zkoss.web.servlet.Servlets;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffPunishListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.staff.manager.UecPunishManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecPunish;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.FileUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecOutStaffPunishListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	@Getter
	private UecOutStaffPunishListboxBean bean = new UecOutStaffPunishListboxBean();

	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");
	/**
	 * 人员处罚情况实体
	 */
	private UecPunish uecPunish;
	/**
	 * 翻页查询条件实体
	 */
	private UecPunish punishPaging;

	private UecPunish approvalPunishPaging;
	/**
	 * 人员处罚情况查询实体
	 */
	private UecPunish qryUecPunish;

	/**
	 * 上传文件
	 */
	private Media media;

	/**
	 * 存放上传数据
	 */
	private List<UecPunish> uecPunishs = new ArrayList<UecPunish>();

	private static final int TOTAL_COLUMN = 8;

	private UecPunishManager uecPunishManager = (UecPunishManager) ApplicationContextUtil
			.getBean("uecPunishManager");

	private List<UecPunish> punishList = new ArrayList<UecPunish>();

	private boolean isOutStaffEntryExt = false;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffPunishListboxExt() {
		Executions.createComponents(
				UecOutStaffConstants.UEC_OUT_STAFF_PUNISH_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY,
				this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY_RESPONSE);
		this.addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY_OF_OUTSTAFF_ENTRY,
				this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY_OF_OUTSTAFF_ENTRY_RESPONSE);
		this.addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY_OF_OUTSTAFF_ENTRY_VIEW,
				this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY_OF_OUTSTAFF_ENTRY_VIEW_RESPONSE);
		this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PUNISH,
				this,
				UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PUNISH_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false, false, false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecPunishList() throws Exception {
		if (this.bean.getUecPunishListbox().getSelectedCount() > 0) {
			uecPunish = (UecPunish) bean.getUecPunishListbox()
					.getSelectedItem().getValue();
		}
	}

	/**
	 * ，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffPunishQueryResponse(ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (null != uecOutStaffInfo) {
	        //设置操作按钮，非盟市主管不可操作按钮
            if(PermissionUtil.isNoMainOutStaff()) {
                this.setButtonValid(false, false, false, false,false,false);
            }else {
                this.setButtonValid(true, true, true, true, true, false);
            }
			qryUecPunish = new UecPunish();
			qryUecPunish.setStaffId(uecOutStaffInfo.getOutStaffId());
			qryUecPunish.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			punishPaging = qryUecPunish;		
			this.onQueryUecPunish();
		}
	}

	/**
	 * 抛出事件， 事件监听,来自入职申请页
	 * 
	 * @param event
	 */
	@SuppressWarnings("unchecked")
	public void onUecOutStaffPunishQueryOfOutStaffEntryResponse(
			ForwardEvent event) {
		uecPunish = null;
		Map<String, Object> map = (Map<String, Object>) event.getOrigin()
				.getData();
		isOutStaffEntryExt = (Boolean) map.get("isOutStaffEntryExt");
		uecOutStaffInfo = (UecOutStaffInfo) map.get("uecOutStaffInfo");
		if (null != uecOutStaffInfo) {
			this.setButtonValid(true, true, true, true, true, false);
			ListModel list = new BindingListModelList(punishList, true);
			// 入职添加，没有入库，展示列表并取消翻页
			this.bean.getUecPunishListbox().setModel(list);
			this.bean.getUecPunishListboxPaging().setTotalSize(1);
		}
	}

	/**
	 * 惩罚审批信息id，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffPunishQueryOfOutStaffEntryViewResponse(
			ForwardEvent event) {
		punishList.clear();
		approvalPunishPaging = new UecPunish();
		String punishIds = (String) event.getOrigin().getData();
		if (!StrUtil.isNullOrEmpty(punishIds)) {
			String[] punishIdArray = punishIds.split("-");
			if (!StrUtil.isNullOrEmpty(punishIdArray)
					&& punishIdArray.length > 0) {
				for (String punishId : punishIdArray) {
					UecPunish uecPunish = UecPunish.newInstance();
					uecPunish.setPunishId(Long.valueOf(punishId));
					uecPunish = uecPunishManager.findUecPunishById(uecPunish);
					punishList.add(uecPunish);
				}

				onQueryUecPunish();
			}
		}

	}

	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecOutStaffPunishResponse(ForwardEvent event) {
		clearVariable();
		uecOutStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecPunishListbox());
		this.bean.getUecPunishListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canDownload, boolean canUpload,
			boolean canView, boolean canAdd, boolean canEdit, boolean candel) {
		this.bean.getDownloadButton().setDisabled(!canDownload);
		this.bean.getUploadButton().setDisabled(!canUpload);
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecPunish() {
		this.bean.getUecPunishListboxPaging().setActivePage(0);
		this.onQueryUecPunishList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecPunish() {
		ListboxUtils.clearListbox(this.bean.getUecPunishListbox());
		this.bean.getUecPunishListboxPaging().setTotalSize(1);
	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecPunish() throws Exception, InterruptedException {
		openUecEditWin("view");
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecPunish() throws Exception {
		openUecEditWin("add");
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecPunish() throws Exception, InterruptedException {
		if (uecPunish != null) {
			openUecEditWin("edit");
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}

	/**
	 * 
	 * @param opType
	 */
	private void openUecEditWin(final String opType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", opType);
		map.put("isOutStaffEntryExt", isOutStaffEntryExt);
		if ("view".equals(opType) || "edit".equals(opType)) {
			if (null != uecPunish) {
				map.put("uecPunish", uecPunish);
			} else {
				ZkUtil.showError("请选择一条记录！", "提示信息");
				return;
			}
		} else if ("add".equals(opType)) {
			map.put("uecOutStaffInfo", uecOutStaffInfo);
		}
		Window window = (Window) Executions.createComponents(
				UecOutStaffConstants.UEC_OUT_STAFF_PUNISH_EDIT_ZUL, this, map);
		try {
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						if ("edit".equals(opType) && isOutStaffEntryExt) {
							punishList.remove(uecPunish);
						}
						// 新增或修改完成后，列表从数据库更新
						uecPunish = (UecPunish) event.getData();
						if (!isOutStaffEntryExt) {
							onQueryUecPunishList();
						} else {
							punishList.add(uecPunish);
							ListModel list = new BindingListModelList(
									punishList, true);
							bean.getUecPunishListbox().setModel(list);
							bean.getUecPunishListboxPaging().setTotalSize(1);

							postPunishList();
						}

					}
				}
			});
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecPunish() {
		if (this.uecPunish.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecPunish.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecPunishListbox(),
								uecPunish, "del");
						uecPunish = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 审批状态中修改
	 */
	public void onApprovalEditUecPunish() {
		if (null != uecPunish) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "approvalEdit");
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			map.put("uecPunish", uecPunish);
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_PUNISH_EDIT_ZUL, this,
					map);
			try {
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecPunish = (UecPunish) event.getData();
							PageInfo pageInfo = uecPunishManager
									.queryPageInfoByUecPunishOnApproval(
											uecPunish,
											bean.getUecPunishListboxPaging()
													.getActivePage() + 1,
											bean.getUecPunishListboxPaging()
													.getPageSize());
							ListModel list = new BindingListModelList(pageInfo
									.getDataList(), true);
							bean.getUecPunishListbox().setModel(list);
							bean.getUecPunishListboxPaging().setTotalSize(1);

						}
					}
				});
			} catch (SuspendNotAllowedException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		} else {
			ZkUtil.showError("请选中要修改的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutPunishListboxPaging() {
		this.onQueryUecPunishList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecPunish = null;
		punishPaging = null;
		punishList.clear();
	}

	/**
	 * 查询
	 */
	public void onQueryUecPunishList() {
		// 翻页查看生效数据
		if (null != punishPaging) {
			PageInfo pageInfo = uecPunishManager.queryPageInfoByUecPunish(
					punishPaging, this.bean.getUecPunishListboxPaging()
							.getActivePage() + 1, this.bean
							.getUecPunishListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecPunishListbox().setModel(list);
			this.bean.getUecPunishListboxPaging().setTotalSize(
					pageInfo.getTotalCount());

		}
		// 翻页查看审批数据
		if (null != approvalPunishPaging) {
//			List<UecPunish> pagePunishs = getCurrentPageList(punishList, bean
//					.getUecPunishListboxPaging().getActivePage() + 1, bean
//					.getUecPunishListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(punishList, true);
			this.bean.getUecPunishListbox().setModel(list);
			this.bean.getUecPunishListboxPaging().setTotalSize(1);
		}

		qryUecPunish = null;
		uecPunish = null;
	}

	/**
	 * 下载模版
	 */
	@SuppressWarnings("deprecation")
	public void onDownloadTemplate() {
		try {
			String charset = "UTF-8";
			// 服务器文件名
			String fileName = "uecOutStaffPunish_template.xls";
			// 编码后文件名
			String encodedName = null;
			encodedName = URLEncoder.encode(fileName, charset);
			// 将空格替换为+号
			encodedName = encodedName.replace("%20", "+");
			HttpServletRequest httpRequest = (HttpServletRequest) Executions
					.getCurrent().getNativeRequest();
			// 解决ie6 bug 或者是火狐浏览器
			if (encodedName.length() > 150 || Servlets.isGecko(httpRequest)
					|| Servlets.isGecko3(httpRequest)) {
				encodedName = new String(fileName.getBytes(charset),
						"ISO8859-1");
			}
			Filedownload.save(
					new FileInputStream(httpRequest
							.getRealPath("/pages_nmg/uec_outStaff/doc/"
									+ fileName)), "application/octet-stream",
					encodedName);
		} catch (Exception e) {
			ZkUtil.showError("下载员工惩罚信息导入模版失败。", "系统提示");
		}
	}

	/**
	 * 文件上传 导入员工
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onUpload$uploadButton(final ForwardEvent forwardEvent)
			throws Exception {

		// 清除变量
		uecPunishs.clear();

		if (!isOutStaffEntryExt) {
			UecTrial qryUecTrial = UecTrial.newInstance();
			qryUecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
			qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
			if (null == qryUecTrial) {
				ZkUtil.showError("此人未入职,请先执行入职操作", "温馨提示");
				return;
			}
		}

		String tips = "";
		if (!ApprovalUtil.getActivityFlag()) {
			tips = "确认批量导入文件？导入后将直接保存到人员信息！";
		} else {
			tips = "确认批量导入文件？导入后将直接发起流程！";
		}
		Messagebox.show(tips, "输入确认", Messagebox.OK | Messagebox.CANCEL,
				Messagebox.QUESTION, new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (((Integer) event.getData()).intValue() == Messagebox.OK) {
							bean.getUecPunishListbox().clearSelection();

							media = ((UploadEvent) forwardEvent.getOrigin())
									.getMedia();
							if (null == media) {
								ZkUtil.showError("请选择要上传的文件!", "系统提示");
								return;
							}
							String fileName = media.getName();
							if (fileName.endsWith(".xls")
									|| fileName.endsWith(".xlsx")) {
								// readsUpLoadFile();
							} else {
								ZkUtil.showError(
										"导入的文件必须是以.xls或.xlsx结尾的EXCEL文件!",
										"系统提示");
								return;
							}
							String[][] objArray = FileUtil.readExcel(media, 1,
									1);
							if (objArray == null || objArray.length == 0) {
								ZkUtil.showError("导入文件没有数据！", "错误信息");
								return;
							}
							setDataToRows(objArray, fileName);
						} else {
							return;
						}
					}
				});

	}

	private void setDataToRows(String[][] data, String fileName) {

		uecPunishs.clear();
		String[] row = null;
		boolean isUpload = true;
		for (int i = 0; i < data.length; i++) {
			row = data[i];
			if (row.length != TOTAL_COLUMN + 1) {
				ZkUtil.showError("导入文件数据不正确！", "错误信息");
				return;
			}

			UecPunish uecPunish = UecPunish.newInstance();
			for (int j = 0; j < TOTAL_COLUMN; j++) {

				switch (j) {
				// 惩罚类型
				case 0:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("惩罚类型为空！", "错误提示");
						isUpload = false;
						return;
					}
					uecPunish.setPunishType(data[i][j]);
					break;
				// 惩罚机构
				case 1:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("惩罚机构为空！", "错误提示");
						isUpload = false;
						return;
					}

					uecPunish.setPunishOrg(data[i][j]);
					break;
				// 惩罚事由
				case 2:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("惩罚事由为空！", "错误提示");
						isUpload = false;
						return;
					}
					uecPunish.setPunishMatter(data[i][j]);
					break;
				// 处罚级别
				case 3:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("处罚级别为空！", "错误提示");
						isUpload = false;
						return;
					}
					if ("地市级".equals(data[i][j])) {
						uecPunish.setGlbdef4("1001A1100000000005HP");
					} else {
						ZkUtil.showError("处罚级别填写不正确!", "错误提示");
						return;
					}
					break;
				// 惩罚措施
				case 4:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("惩罚措施为空！", "错误提示");
						isUpload = false;
						return;
					}
					uecPunish.setPunishMeas(data[i][j]);
					break;
				case 5:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("惩罚时间为空！", "错误提示");
						isUpload = false;
						return;
					}
					try {
						new SimpleDateFormat("yyyy-MM-dd").parse(data[i][j]);
					} catch (ParseException e) {
						e.printStackTrace();
						ZkUtil.showError("惩罚时间格式不正确！", "错误提示");
						isUpload = false;
						return;
					}
					uecPunish.setPunishDate(data[i][j]);
					break;
				// 撤销处分时间
				case 6:
//					if (StrUtil.isNullOrEmpty(data[i][j])) {
//						ZkUtil.showError("撤销处分时间为空！", "错误提示");
//						isUpload = false;
//						return;
//					}
					try {
						if (!StrUtil.isNullOrEmpty(data[i][j])) {
							new SimpleDateFormat("yyyy-MM-dd").parse(data[i][j]);
						}
					} catch (ParseException e) {
						e.printStackTrace();
						ZkUtil.showError("撤销处分时间格式不正确！", "错误提示");
						isUpload = false;
						return;
					}
					uecPunish.setGlbdef2(data[i][j]);
					break;
				// 撤销处分文件
				case 7:
//					if (StrUtil.isNullOrEmpty(data[i][j])) {
//						ZkUtil.showError("撤销处分文件为空！", "错误提示");
//						isUpload = false;
//						return;
//					}
					uecPunish.setGlbdef3(data[i][j]);
					break;
				default:
					break;
				}

			}
			if (!isUpload) {
				return;
			}

			uecPunish.setStaffId(uecOutStaffInfo.getOutStaffId());
			if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
				ZkUtil.showError("当前用户不存在！", "错误提示");
				return;
			}

			uecPunish.setCreator(PlatformUtil.getCurrentUser().getScreenName());
			uecPunish.setCreationtime(new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss").format(new Date()));
			uecPunish.setPkOrg(uecOutStaffInfo.getPkHrorg());
			uecPunish.setDr(0L);
			uecPunish.setLastflag("Y");

			uecPunish.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);

			uecPunishs.add(uecPunish);
		}

		if (null != uecPunishs) {
			punishList.addAll(uecPunishs);
			ListModel list = new BindingListModelList(uecPunishs, true);
			this.bean.getUecPunishListbox().setModel(list);
			this.bean.getUecPunishListboxPaging().setTotalSize(1);
			if (isOutStaffEntryExt) {
				postPunishList();
			} else {
				if (ApprovalUtil.getActivityFlag()) {
					String formTheme = "盟市外包人员惩罚批量申请流程";
					String procInstId = null;
					for (UecPunish uecPunish : uecPunishs) {

						// 发起流程 uec_punish 新增一条记录
						// 判断该人员是否在审状态，如果是在审状态不允许再次发起此流程
						// boolean isOnApproval =
						// ApprovalUtil.isOnApproval(procDefId,
						// uecOutStaffInfo.getOutStaffId());
						// if (isOnApproval) {
						// ZkUtil.showError("在审状态不允许发起！", "错误提示");
						// return;
						// }

						Long outStaffId = uecOutStaffInfo.getOutStaffId();

						uecPunish.addStatusCdIS3();
						Long punishId = uecPunish.getPunishId();

						if (StrUtil
								.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
							ZkUtil.showError("当前登录用户不存在!", "错误提示");
							return;
						}
						String currentName = PlatformUtil.getCurrentUser()
								.getScreenName();

						String startFlowJson = "{" + "formTheme: '"
								+ formTheme
								+ "',"
								+ " formContent: '惩罚人员:"
								+ uecOutStaffInfo.getPsnName()
								+ "',"
								+ " procDefId: '"
								+ ApprovalConstants.OUT_STAFF_PUNISH_PROC_DEF_ID
								+ "'," + " urgent: '1',"
								+ " atiActCategoryId: '"
								+ ApprovalConstants.MODIFICATION_CATEGORY_ID
								+ "'," + " procDefKey: '"
								+ ApprovalUtil.OUT_STAFF_PUNISH_EDIT + "',"
								+ " formSender: '" + currentName + "',"
								+ " primaryId: '{" + "uec_out_staff_info_his:"
								+ outStaffId + ",uec_out_staff_info:"
								+ outStaffId + ",uec_punish:" + punishId + "'}";

						try {
							procInstId = ApprovalUtil
									.startApproval(startFlowJson);
						} catch (Exception e) {
							// 流程发起失败 数据改为失效
							uecPunish.removeOnly();
							onResetUecPunish();
							ZkUtil.showError("流程发起失败！", "错误提示");
							e.printStackTrace();
						}
						if (StrUtil.isNullOrEmpty(procInstId)) {
							uecPunish.removeOnly();
							onResetUecPunish();
							ZkUtil.showError("流程发起失败！", "错误提示");
							return;
						}
						// 第一审批人如果是发起人直接到人力审批环节
						ApprovalUtil.isCurrentAssignNames(procInstId);

					}
					ApprovalUtil.showNextAssignee(procInstId, formTheme);
				} else {
					for (UecPunish uecPunish : uecPunishs) {
						uecPunish.addOnly();
					}
					ZkUtil.showInformation("批量导入惩罚信息已保存成功！", "温馨提示");
				}
			}
		}
	}

	/**
	 * 抛出事件，把添加的列表发送给OutStaffEntryExt
	 */
	private void postPunishList() {
		Events.postEvent("onPostPunishListOfOutStaffEntry", this, punishList);
	}

	/**
	 * 获取当前页list
	 * 
	 * @param list
	 * @param activePage
	 * @param pageSize
	 * @return
	 */
	public List<UecPunish> getCurrentPageList(List<UecPunish> list, int activePage,
			int pageSize) {
		int fromIndex = activePage * pageSize;
		int toIndex = fromIndex + pageSize;
		if (fromIndex >= list.size()) {
			return null;
		}
		if (toIndex >= list.size()) {
			return list.subList(fromIndex, list.size());
		}
		return list.subList(fromIndex, toIndex);

	}
}
